package com.zhku.laosiji.controller;

import com.zhku.laosiji.pojo.Notice;
import com.zhku.laosiji.pojo.User;
import com.zhku.laosiji.service.NoticeService;
import com.zhku.laosiji.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/** 用户管理控制层
 * @author Jiayuan
 * @version 1.0
 * @description:
 * @time 9/5/2018 12:13 PM
 */

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private NoticeService noticeService;

    @RequestMapping("/useraddpage")
    public ModelAndView insert(){
        return new ModelAndView("/user-add") ;
    }

    @RequestMapping("/startuser")
    @ResponseBody
    public String startUser(int id){
        userService.startUser(id);
        return "user-list";
    }

    @RequestMapping("/stopuser")
    @ResponseBody
    public String stopUser(int id){
        userService.stopUser(id);
        return "user-list";
    }

    @RequestMapping("/insertUser")
    public String insertUser(User user){
        if(user!=null){
            user.setCreatedate(new Date());
            user.setStauts(0);
        }
        userService.insert(user);
        return "redirect:/user/users";
        //return new ModelAndView("user-list");
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(int id){
        userService.deleteUserById(id);
        return "user-list";
    }

    @RequestMapping("/getuser")
    public ModelAndView getUser(@RequestParam("search")String search,Map<String,Object> map){
       List<User> user = userService.selectUserByNameOrTel(search);
        map.put("users",user);

       return new ModelAndView("user-list",map);
    }


    @GetMapping("/users")
    public ModelAndView getUserList(){
        Collection<User> users = userService.getUesrList();
        ModelAndView modelAndView = new ModelAndView("user-list");
        modelAndView.addObject("users",users);
        return modelAndView;
    }

    @GetMapping("/select")
    public ModelAndView select(@RequestParam("id")Integer id,Map<String,Object> map){
        User user = userService.selectByPrimaryKey(id);
        if(user!=null){
            map.put("user",user.getName());
        }else{
            map.put("user","查无此人");
        }
        return new ModelAndView("/user/test",map);

    }

    @PostMapping("/login")
    public String login(User user,
                        Model model,
                        HttpSession session) {
        User u1 = userService.selectByName(user.getName());
        if (u1 == null) { //判断账号是否存在
            model.addAttribute("loginError", true);
            model.addAttribute("errormsg","账号不存在！");
            return "user/join";
        }
        //验证密码正确
        if (u1.getPassword().equals(user.getPassword())) {
            session.setAttribute("user",u1);
            return "redirect:/user/index";
        }else {
            model.addAttribute("loginError", true);
            model.addAttribute("errormsg","账号或密码错误！");
            return "user/join";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/user/index";
    }

    @PostMapping("/register")
    public String register(User user,
                           Model model) {
        User u1 = userService.selectByName(user.getName());
        if (u1 == null) {
            userService.insertUser(user);
            return "user/join";
        } else {
            model.addAttribute("loginError", true);
            model.addAttribute("errormsg","该账号已被注册！");
            return "user/join";
        }
    }

    //所有访问index页面统一通过这个跳转，能够把模板数据加载到页面   by jiayuan
    @RequestMapping("/index")
    public ModelAndView index(HttpSession session,Map<String,Object> map){
        if(session.getAttribute("user") !=null){
            map.put("user",((User)session.getAttribute("user")));
        }

        List<Notice> noticeList = noticeService.selectAllByTop10();
        map.put("noticeList", noticeList);
        return new ModelAndView("/user/index",map);
    }

    //个人中心  by jiayaun
    @RequestMapping("/person")
    public ModelAndView person(HttpSession session,Map<String,Object> map){
        if(session.getAttribute("user") != null){
            map.put("user",((User)session.getAttribute("user")));
            return new ModelAndView("/user/car/user-show",map);
        }else{
            return new ModelAndView("user/join");
        }
    }

    //修改信息 by jiayuan
    @RequestMapping("/update")
    public ModelAndView update(User user,HttpSession session,Map<String,Object> map){
        if(session.getAttribute("user") != null){
            user.setId(((User)session.getAttribute("user")).getId());
            userService.update(user);
            User updateUser = userService.selectByPrimaryKey(((User)session.getAttribute("user")).getId());
            map.put("user",updateUser);
            session.setAttribute("user",updateUser);
            return new ModelAndView("/user/car/user-show",map);
        }else {
            return new ModelAndView("user/join");
        }
    }

    //修改密码  (暂未处理)   by jiayuan
    @RequestMapping("/updatePass")
    public ModelAndView updatePass(HttpSession session){

        return new ModelAndView("/user/car/password");
    }

    @PostMapping("/updatePwd")
    public String updatePwd(@RequestParam("oldpassword") String oldpassword,
                            @RequestParam("newpassword") String newpassword,
                            HttpSession session,
                            Model model) {
        try {
            //从session获取当前登录用户
            User user = (User) session.getAttribute("user");
            if (user.getPassword().equals(oldpassword)) {//判断密码是否正确
                user.setPassword(newpassword);
                session.setAttribute("user", user);
                userService.update(user);
                model.addAttribute("loginError", true);
                model.addAttribute("errormsg","修改密码成功！");
            }else{
                model.addAttribute("loginError", true);
                model.addAttribute("errormsg","密码错误，修改密码失败！");
            }
        } catch (Exception e) {
            model.addAttribute("loginError", true);
            model.addAttribute("errormsg","修改密码失败！");
        }

        return "/user/car/password";
    }

}
